package com.kuang.util;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 马自辉
 * @date 2021/1/19 10:34
 * @description:
 */
public class Md5 {
    /**
     * 十六进制下数字到字符的映射数组
     */
    private final static String[] HEX_DIGITS = {"0", "1", "2", "3", "4",
            "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * md5加密算法
     * @param  originString 待加密字符串
     * @return 加密后的字符串
     */
    public static String encodeByMD5(String originString){
        if (originString != null){
            try{
                //创建具有指定算法名称的信息摘要
                MessageDigest md = MessageDigest.getInstance("MD5");
                //使用指定的字节数组对摘要进行最后更新，然后完成摘要计算
                byte[] results = md.digest(originString.getBytes());
                //将得到的字节数组变成字符串返回
                String resultString = byteArrayToHexString(results);
                return resultString.toUpperCase();
            } catch(Exception ex){
                ex.printStackTrace();
            }
        }
        return null;
    }

    // public static void main(String[] args) {
    //     String dae = DateUtils.dateTimeNow();
    //     System.out.println(dae);
    //    String str =  encodeByMD5("ZYUNI666"+encodeByMD5("3c439fe5c03c4dfdbc00ff11b6ff5aa2")+ dae);
    //     System.out.println(str);
    // }

    /**
     *  MD5加密小写
     * @param password
     * @return
     */
    public static String MD5Encryption(String password) {
        StringBuffer hexString = new StringBuffer();
        if (password != null && password.trim().length() != 0) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(password.getBytes(StandardCharsets.UTF_8));
                byte[] hash = md.digest();
                for (int i = 0; i < hash.length; i++) {
                    if ((0xff & hash[i]) < 0x10) {
                        hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
                    } else {
                        hexString.append(Integer.toHexString(0xFF & hash[i]));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return hexString.toString();
    }

    /**
     * 转换字节数组为十六进制字符串
     * @param
     * @return    十六进制字符串
     */
    private static String byteArrayToHexString(byte[] b){
        StringBuffer resultSb = new StringBuffer();
        for (byte value : b) {
            resultSb.append(byteToHexString(value));
        }
        return resultSb.toString();
    }

    /** 将一个字节转化成十六进制形式的字符串     */
    private static String byteToHexString(byte b){
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return HEX_DIGITS[d1] + HEX_DIGITS[d2];
    }

    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = simpleDateFormat.format(new Date());
        //appId
        System.err.println("appId:"+"\n"+"ZYUNI666");
        //reqTime
        System.err.println("reqTime:"+"\n"+format);
        String dcsEncode = Md5.encodeByMD5("ZYUNI666" + Md5.encodeByMD5("3c439fe5c03c4dfdbc00ff11b6ff5aa2") + format);
        //sign
        System.err.println("sign:"+"\n"+dcsEncode);
        //param
        System.err.println("param:"+"\n"+"{\"字段名\":\"值\"}");
    }

}
